﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Net.Mime;
using System.Net;

namespace SMES.Common
{
    //此类用于发送邮件通用类
    public class EmailSendHelper
    {
        //特别注意如果使用的是163的邮箱首先需要设置开启Smtp服务器,然后设置客户端授权码,同时将授权码当做密码赋值
        //163企业邮箱已经开启了smtp服务器
        private string smtpAddress;
        private int smtpPort;
        private string fromEmail;
        private string toEmail;//邮件接收人,多人的话用;隔开
        private string passwordEmail;//发送邮箱的密码
        public EmailSendHelper(string smtpAddress,int smtpPort,string fromEmail,string toEmail,string passwordEmail)
        {
            this.smtpAddress = smtpAddress;
            this.smtpPort = smtpPort;
            this.fromEmail = fromEmail;
            this.toEmail = toEmail;
            this.passwordEmail = passwordEmail;
        }
        /// <summary>
        /// 用于发送邮件--支持添加附件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="content">正文</param>
        /// <param name="attachmentsPath">附件路径</param>
        /// <returns></returns>
        public bool SendMessage(string subject, string content, string attachmentsPath)
        {
            try
            {
                SmtpClient sc = new SmtpClient(smtpAddress, smtpPort);
                sc.Credentials = new NetworkCredential(fromEmail, passwordEmail);
                sc.EnableSsl = false;//必须设置为false
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress(fromEmail);
                mm.To.Add(toEmail);
                mm.Subject = subject;
                mm.Body = content;
                mm.IsBodyHtml = false;//设置内容不是html格式
                AddAttachments(attachmentsPath, mm);
                sc.Send(mm);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 用于发送信息--不支持添加附件
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool SendMessage(string subject, string content)
        {
            try
            {
                SmtpClient sc = new SmtpClient(smtpAddress, smtpPort);
                sc.Credentials = new NetworkCredential(fromEmail, passwordEmail);
                sc.EnableSsl = false;//必须设置为false
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress(fromEmail);
                mm.To.Add(toEmail);
                mm.Subject = subject;
                mm.Body = content;
                mm.IsBodyHtml = false;//设置内容不是html格式
                sc.Send(mm);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        ///<summary>
        /// 添加附件
        ///</summary>
        ///<param name="attachmentsPath">附件的路径集合，以分号分隔</param>
        private void AddAttachments(string attachmentsPath, MailMessage mMailMessage)
        {
            try
            {
                string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义
                Attachment data;
                ContentDisposition disposition;
                for (int i = 0; i < path.Length; i++)
                {
                    data = new Attachment(path[i], MediaTypeNames.Application.Octet);
                    disposition = data.ContentDisposition;
                    disposition.CreationDate = File.GetCreationTime(path[i]);
                    disposition.ModificationDate = File.GetLastWriteTime(path[i]);
                    disposition.ReadDate = File.GetLastAccessTime(path[i]);
                    mMailMessage.Attachments.Add(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
    public class EmailServerProvider
    {
        //新浪POP3服务器信息
        public const string SinaPopAddress = "pop3.sina.com.cn";
        public const int SinaPopPort = 110;
        //新浪SMTP服务器信息
        public const string SinaSmtpAddress = "smtp.sina.com.cn";
        public const int SinaSmtpPort = 25;
        //新浪VIPPOP3服务器信息
        public const string SinaVipPopAddress = "pop3.vip.sina.com";
        public const int SinaVipPopPort = 110;
        //新浪VIPSMTP服务器信息
        public const string SinaVipSmtpAddress = "smtp.vip.sina.com";
        public const int SinaVipSmtpPort = 25;
        //SohuPOP3服务器信息
        public const string SohuPopAddress = "pop3.sohu.com";
        public const int SohuPopPort = 110;
        //SohuSMTP服务器信息
        public const string SohuSmtpAddress = "smtp.sohu.com";
        public const int SohuSmtpPort = 25;
        //126POP3服务器信息
        public const string Mail126PopAddress = "pop.126.com";
        public const int Mail126PopPort = 110;
        //126SMTP服务器信息
        public const string Mail126SmtpAddress = "smtp.126.com";
        public const int Mail126SmtpPort = 25;
        //139POP3服务器信息
        public const string Mail139PopAddress = "pop.139.com";
        public const int Mail139PopPort = 110;
        //139SMTP服务器信息
        public const string Mail139SmtpAddress = "smtp.139.com";
        public const int Mail139SmtpPort = 25;
        //163POP3服务器信息
        public const string Mail163PopAddress = "pop.163.com";
        public const int Mail163PopPort = 110;
        //163SMTP服务器信息
        public const string Mail163SmtpAddress = "smtp.163.com";
        public const int Mail163SmtpPort = 25;
        //163企业POP3服务器信息
        public const string Mail163ExPopAddress = "pop.qiye.163.com";
        public const int Mail163ExPopPort = 110;
        //163企业SMTP服务器信息
        public const string Mail163ExSmtpAddress = "smtp.qiye.163.com";
        public const int Mail163ExSmtpPort = 25;
        //QQPOP3服务器信息
        public const string QQPopAddress = "pop.qq.com";
        public const int QQPopPort = 110;
        //QQSMTP服务器信息
        public const string QQSmtpAddress = "smtp.qq.com";
        public const int QQSmtpPort = 25;
        //QQ企业POP3服务器信息
        public const string QQExPopAddress = "pop.exmail.qq.com";
        public const int QQExPopPort = 995;
        //QQ企业SMTP服务器信息
        public const string QQExSmtpAddress = "smtp.exmail.qq.com";
        public const int QQExSmtpPort = 587;
    }
}
